<template>
  <div>
    <div class="title">实施盘点</div>
    <base-list ref="cheackPlan"
               :condition="condition"
               :datas.sync="cheackPlanData"
               :pageSearch="pageSearch">
      <el-form :inline="true"
               :model="condition"
               class="search-form"
               slot="search"
               ref="aList"
               label-width="80px">
        <el-row>
          <el-col :span="6"
                  class="search-width6">
            <el-form-item label="盘点计划状态"
                          label-width="100px">
              <el-select v-model="condition['search_EQ_auditStatus']"
                         placeholder="请选择"
                         clearable
                         filterable>
                <el-option v-for="[key, val] in _dictAuditStatus"
                           :key="key"
                           :label="val"
                           :value="key"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6"
                  class="search-width6">
            <el-form-item label="盘点计划号"
                          label-width="100px">
              <el-input type="text"
                        v-model="condition['search_EQ_planNo']"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width6">
            <el-form-item label="所属仓库">
              <el-select v-model="condition['search_EQ_warehouseCode']"
                         placeholder="请选择">
                <el-option v-for="(item,index) in warehouseList"
                           :key="index"
                           :label="item.name"
                           :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width6">
            <el-form-item label="库房">
              <auto-search-room-name @select="v=>condition['search_LIKE_roomName']=v"></auto-search-room-name>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button type="primary"
                       icon="el-icon-search"
                       class="ml-80"
                       @click="loadData">查 询</el-button>
            <el-button @click="handleRouterPush({path:'/goods/matter/wrongList',name:'盘点有误物资清单'})">盘点有误物资清单</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-row slot="status">
        <el-col :span="14">
          <div class="control-line">
            盘点计划列表
          </div>
        </el-col>
      </el-row>
      <el-table slot="list"
                :data="cheackPlanData"
                border
                stripe
                highlight-current-row
                @current-change="handleCurrentChange"
                max-height="550">
        <el-table-column label="序号"
                         type="index"
                         width="50"></el-table-column>
        <el-table-column prop="planNo"
                         label="盘点计划编号"
                         min-width="200"></el-table-column>
        <el-table-column prop="status"
                         label="状态"
                         min-width="200">
          <template slot-scope="scope">
            {{ scope.row.status | _filterJobStatusFilter }}
          </template>
        </el-table-column>
        <el-table-column prop="warehouseName"
                         label="盘点仓库"
                         min-width="200"></el-table-column>
        <el-table-column prop="roomName"
                         label="盘点库房"
                         min-width="150"></el-table-column>
        <el-table-column prop="totalNum"
                         label="盘点件数"
                         min-width="120"></el-table-column>
        <el-table-column prop="totalNum"
                         label="盘点重量"
                         min-width="120"></el-table-column>
        <el-table-column prop="totalCount"
                         label="盘点数量"
                         min-width="120"></el-table-column>
        <el-table-column prop="remark"
                         label="备注"
                         min-width="120"></el-table-column>
        <el-table-column prop="startTime"
                         label="盘点开始时间"
                         min-width="120"></el-table-column>
        <el-table-column prop="endTime"
                         label="盘点结束时间"
                         min-width="120"></el-table-column>
        <el-table-column fixed="right"
                         align="center"
                         label="操作"
                         width="240">
          <template slot-scope="scope">
            <el-button type="text"
                       class="danger"
                       size="small"
                       @click.stop="handleRemove(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </base-list>
    <div v-show="show">
      <el-row>
        <el-col :span="14">
          <div class="control-line">
            盘点详情
          </div>
        </el-col>
      </el-row>
      <el-menu :default-active="activeIndex"
               class="el-menu-demo"
               mode="horizontal"
               @select="handleSelect">
        <el-menu-item index="1">盘点明细列表</el-menu-item>
        <el-menu-item index="2">盘点有误物资清单<span class="wrongNum">{{ wrongNum }}</span></el-menu-item>

      </el-menu>
      <!-- 盘点明细列表 -->
      <el-table :data="cheackDetailData"
                v-show="showList"
                border
                stripe
                max-height="550">
        <el-table-column label="序号"
                         type="index"
                         width="50"></el-table-column>
        <el-table-column prop="status"
                         label="状态"
                         min-width="200">
          <template slot-scope="scope">
            {{ scope.row.status | _filterCheckStatus }}
          </template>
        </el-table-column>
        <el-table-column prop="cargoName"
                         label="品名"
                         min-width="200"></el-table-column>
        <el-table-column prop="normStr"
                         label="规格"
                         min-width="120"></el-table-column>
        <el-table-column prop="material"
                         label="材质"
                         min-width="120"></el-table-column>
        <el-table-column prop="madeBy"
                         label="生产厂家"
                         min-width="120"></el-table-column>
        <el-table-column prop="inventoryNum"
                         label="盘点件数"
                         min-width="120"></el-table-column>
        <el-table-column prop="inventoryWeight"
                         label="盘点重量"
                         min-width="120"></el-table-column>
        <el-table-column prop="inventoryCount"
                         label="盘点数量"
                         min-width="120"></el-table-column>
        <el-table-column align="center"
                         label="操作"
                         width="240">
          <template slot-scope="scope">
            <el-button size="small"
                       type="text"
                       :disabled="scope.row.status==='OPERATED' || scope.row.status==='OPERATING'"
                       @click="confirm(scope.row)">
              盘点确认
            </el-button>
            <el-button size="small"
                       type="text"
                       :disabled="scope.row.status==='OPERATED'"
                       @click="handleDialogForm(scope.row)">
              细盘
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 盘点有误物资清单 -->
      <!-- <el-form :inline="true"
                 :model="condition"
                 class="search-form"
                 label-width="80px">
          <el-row>
            <el-col :span="6"
                    class="search-width6">
              <el-form-item label="跟踪号">
                <auto-search-org v-model="condition['search_LIKE_trackingNo']"
                                 placeholder="请选择"
                                 @selectName="v=>condition['search_LIKE_trackingNo']=v"
                                 :allOrg="true"
                                 :name="form.controlOrgName"></auto-search-org>
              </el-form-item>
            </el-col>
            <el-col :span="6"
                    class="search-width6">
              <el-form-item label="处理状态">
                <el-select v-model="condition['search_EQ_status']"
                           placeholder="请选择"
                           clearable
                           filterable>
                  <el-option v-for="[key, val] in _dictAuditStatus"
                             :key="key"
                             :label="val"
                             :value="key"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="2">
              <el-button type="primary"
                         icon="el-icon-search"
                         class="ml-80"
                         @click="loadData">查 询</el-button>
            </el-col>
          </el-row>
        </el-form> -->
      <el-table :data="cheackWrongData"
                v-show="showWrong"
                border
                stripe
                max-height="550">
        <el-table-column label="序号"
                         type="index"
                         width="50"></el-table-column>
        <el-table-column prop="trackingNo"
                         label="跟踪号"
                         min-width="200"></el-table-column>
        <el-table-column prop="exceptionMsg"
                         label="有误信息"
                         min-width="200"></el-table-column>
        <el-table-column prop="status"
                         label="处理状态"
                         min-width="200">
          <template slot-scope="scope">
            {{ scope.row.status | _filterHandleStatus }}
          </template>
        </el-table-column>
        <el-table-column prop="cargoName"
                         label="盘点品名"
                         min-width="200"></el-table-column>
        <el-table-column prop="inventoryNum"
                         label="件数"
                         min-width="150"></el-table-column>
        <el-table-column prop="inventoryWeight"
                         label="重量"
                         min-width="120"></el-table-column>
        <el-table-column prop="inventoryCount"
                         label="数量"
                         min-width="120"></el-table-column>
        <el-table-column prop="normStr"
                         label="规格"
                         min-width="120"></el-table-column>
        <el-table-column prop="material"
                         label="材质"
                         min-width="120"></el-table-column>
        <el-table-column prop="madeBy"
                         label="生产厂家"
                         min-width="120"></el-table-column>
        <el-table-column prop="roomLocation"
                         label="库位"
                         min-width="120"></el-table-column>
        <el-table-column prop="jobBy"
                         label="盘点人"
                         min-width="120"></el-table-column>
        <el-table-column prop="jobTime"
                         label="盘点时间"
                         min-width="120"></el-table-column>
        <el-table-column prop="handleBy"
                         label="处理人"
                         min-width="120"></el-table-column>
        <el-table-column prop="handleTime"
                         label="处理时间"
                         min-width="120"></el-table-column>
        <el-table-column prop="urls"
                         label="上传信息"
                         min-width="120"></el-table-column>
      </el-table>

    </div>
    <div class="back">
      <el-button plain
                 type="info"
                 @click="handleRouterPush({path:'/goods/matter',name:'实物管理工作台'})">返回</el-button>
    </div>
    <detail-count ref="detailCount"></detail-count>
  </div>
</template>

<script>
import { getWarehouseList } from '@/api/adminWarehouse/adminWarehouse' //所属仓库来源于仓库设置
import autoSearchRoomName from '@/components/autocomplete/autoSearchRoomName'
import detailCount from './detailCount'
import {
  getCheackPlanList,
  getCheackDetailList,
  getCheackWrongList,
  getCheackCount,
  deleteCheackPlan,
  resureCount
} from '@/api/matter/cheackPlan'
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import autoSearchName from '@/components/autocomplete/autoSearchName'

export default {
  name: 'cheackPlan',
  components: {
    autoSearchOrg,
    autoSearchName,
    detailCount,
    autoSearchRoomName
  },
  data() {
    return {
      form: this.clearForm(),
      disabled: false,
      condition: {}, // 查询条件
      cheackPlanData: [], // table数组
      cheackDetailData: [], // table数组
      cheackWrongData: [], // table数组
      warehouseList: null, //仓库下拉菜单
      pageSearch: getCheackPlanList, // 分页查询api接口
      id: null,
      activeIndex: '1',
      show: false,
      showList: true, //盘点明细列表
      showWrong: false, //盘点有误物资清单
      planNo: null,
      wrongNum: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.loadData()
    })
    //获取仓库
    getWarehouseList().then(res => {
      this.warehouseList = res.data.aaData
    })
  },
  methods: {
    clearForm() {
      return {
        orgId: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.name
      }
    },
    loadData() {
      this.$refs.cheackPlan.loadData()
    },
    //细盘
    handleDialogForm(item) {
      this.$refs.detailCount.getPlanNo(this.planNo, item.detailedSonVoListId)
      this.$refs.detailCount.dialogForm = true
    },
    //单击盘点计划列表
    handleCurrentChange(val) {
      this.planNo = val.planNo
      this.show = true
      getCheackDetailList(this.planNo).then(res => {
        this.cheackDetailData = res.data.aaData
      })
      //盘点有误数量
      getCheackCount(this.planNo).then(res => {
        this.wrongNum = res.data.mistakeCount
      })
    },
    //盘点详情切换
    handleSelect(key, keyPath) {
      if (key == 1) {
        this.showWrong = false
        this.showList = true
      } else {
        this.showList = false
        this.showWrong = true
        getCheackWrongList({ search_EQ_planNo: this.planNo }).then(res => {
          this.cheackWrongData = res.data.aaData
        })
      }
    },
    // 盘点确认
    confirm(item) {
      let data = item.detailedSonVoListId
      let detailIds = []
      for (let item of data) {
        detailIds.push(item.id)
      }

      this.$confirm('确定对物资进行盘点确认吗？', '盘点确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          resureCount({ detailIds: detailIds, planNo: this.planNo }).then(
            res => {
              this.$message({
                type: 'success',
                message: '盘点确认成功!'
              })
              this.loadData()
            }
          )
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消提交'
          })
        })
    },
    //细盘
    detailCount() {},
    //删除
    handleRemove(item) {
      this.$confirm(`是否删除该数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteCheackPlan(item.planNo).then(res => {
            this.$message({
              type: 'success',
              message: `删除成功!`
            })
            this.loadData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    //返回
    handleRouterPush(val) {
      this.$store.commit('ADD_CACHED_VIEWS', val)
      this.$router.push(val.path)
    }
  }
}
</script>

<style scoped>
.demo {
  position: relative;
}
.wrongNum {
  position: absolute;
  top: 18px;
  width: 15px;
  line-height: 20px;
  padding-left: 7px;
  background: #f50627;
  border-radius: 50px;
  color: #fff;
}
</style>
